<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'check:m8OperCheckDel:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="70%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <h3> 【 订单编号：{{ info.ccode }} 图号：{{ info.picno }} 图名：{{ info.picname }} 】 </h3>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsCheckM8OperCheckDelForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import {
    M8OperCheckDel,
    m8OperCheckDelSave,
    m8OperCheckDelForm,
    listTreeData,
  } from '/@/api/m8/order/m8OperCheckDel';
  import { officeTreeData } from '/@/api/sys/office';

  const emit = defineEmits(['success', 'register']);

  let info = ref<any>({
    checkId: '',
    ccode: '',
    picId: '',
    picno: '',
    picname: '',
    ope: '',
  });

  const { t } = useI18n('check.m8OperCheckDel');
  const { showMessage } = useMessage();
  const record = ref<M8OperCheckDel>({} as M8OperCheckDel);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增不良品处理单') : t('编辑不良品处理单'),
  }));

  const [registerForm, { resetFields, setFieldsValue, validate, setProps }] = useForm();

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    info.value = data;
    setDrawerProps({ loading: true });
    const res = await m8OperCheckDelForm(data);
    record.value = (res.m8OperCheckDel || {}) as M8OperCheckDel;
    record.value.__t = new Date().getTime();
    info.value.ccode = record.value.order.ccode;
    info.value.picno = record.value.orderPic.picno;
    info.value.picname = record.value.orderPic.picname;
    info.value.picId = record.value.orderPic.id;
    setProps({
      labelWidth: 120,
      schemas: [
        {
          label: t('处理单号'),
          field: 'djNo',
          component: 'Input',
          dynamicDisabled: true,
          componentProps: {
            maxlength: 64,
          },
          show: true,
        },
        {
          label: t('检验单号'),
          field: 'checkId',
          component: 'Input',
          componentProps: {
            maxlength: 64,
          },
          dynamicDisabled: true,
          show: true,
        },
        {
          label: t('基本信息'),
          field: 'info1',
          component: 'FormGroup',
          colProps: { lg: 24, md: 24 },
        },
        {
          label: t('责任工序'),
          field: 'operCode',
          component: 'TreeSelect',
          // dynamicDisabled: true,
          componentProps: {
            api: listTreeData,
            params: { picId: info.value.picId },
          },
          required: true,
        },
        {
          label: t('处理数量'),
          field: 'iqty',
          component: 'Input',
          dynamicDisabled: info.value.ope == 0 ? false : true,
          componentProps: {
            maxlength: 8,
          },
          rules: [{ required: true }, { pattern: /^\d+$/, message: t('请输入一个正整数') }],
        },
        {
          label: t('是否需要纠正预防措施'),
          field: 'isNeedYf',
          component: 'RadioGroup',
          dynamicDisabled: info.value.ope == 0 ? false : true,
          componentProps: {
            dictType: 'sys_yes_no',
          },
          labelWidth: 200,
        },
        {
          label: t('是否需要客户审理'),
          field: 'isNeedCus',
          component: 'RadioGroup',
          dynamicDisabled: info.value.ope == 0 ? false : true,
          componentProps: {
            dictType: 'sys_yes_no',
          },
          labelWidth: 220,
          colProps: { lg: 12, md: 12 },
        },
        {
          label: t('不合格品信息'),
          field: 'info2',
          component: 'FormGroup',
          colProps: { lg: 24, md: 24 },
        },
        {
          label: t('不合格品描述'),
          field: 'checkRemarks',
          component: 'InputTextArea',
          dynamicDisabled: info.value.ope == 0 ? false : true,
          componentProps: {
            maxlength: 500,
            rows: 4,
          },
          colProps: { lg: 24, md: 24 },
          required: true,
        },
        {
          label: t('检验员'),
          field: 'checkor',
          fieldLabel: 'checkorName',
          component: 'TreeSelect',
          dynamicDisabled: info.value.ope == 0 ? false : true,
          componentProps: {
            api: officeTreeData,
            params: { isLoadUser: true, userIdPrefix: '', isAll: true, isShowCode: true },
            canSelectParent: false,
            allowClear: true,
          },
          colProps: { lg: 24, md: 24 },
        },
        {
          label: t('不合格品原因分析'),
          field: 'info3',
          component: 'FormGroup',
          colProps: { lg: 24, md: 24 },
        },
        {
          label: t('原因分析'),
          field: 'operReson',
          component: 'InputTextArea',
          dynamicDisabled: info.value.ope == 1 || info.value.ope == '0' ? false : true,
          componentProps: {
            maxlength: 500,
            rows: 4,
          },
          colProps: { lg: 24, md: 24 },
          // required: true,
        },
        {
          label: t('操作员'),
          field: 'operator',
          fieldLabel: 'operatorName',
          component: 'TreeSelect',
          dynamicDisabled: info.value.ope == 1 || info.value.ope == '0' ? false : true,
          componentProps: {
            api: officeTreeData,
            params: { isLoadUser: true, userIdPrefix: '', isAll: true, isShowCode: true },
            canSelectParent: false,
            allowClear: true,
          },
        },
        {
          label: t('操作员确认日期'),
          field: 'operatorSubDate',
          component: 'DatePicker',
          dynamicDisabled: info.value.ope == 1 || info.value.ope == '0' ? false : true,
          componentProps: {
            format: 'YYYY-MM-DD HH:mm',
            showTime: { format: 'HH:mm' },
          },
          labelWidth: 180,
        },
        {
          label: t('不合格品处理方案'),
          field: 'info4',
          component: 'FormGroup',
          colProps: { lg: 24, md: 24 },
        },
        {
          label: t('处理意见类型'),
          field: 'dealType',
          component: 'RadioGroup',
          dynamicDisabled: info.value.ope == 2 || info.value.ope == '0' ? false : true,
          componentProps: {
            dictType: 'm8_deal_type',
            allowClear: true,
          },
          colProps: { lg: 24, md: 24 },
        },
        {
          label: t('处理方案'),
          field: 'dealRemarks',
          component: 'InputTextArea',
          dynamicDisabled: info.value.ope == 2 || info.value.ope == '0' ? false : true,
          componentProps: {
            maxlength: 500,
            rows: 4,
          },
          colProps: { lg: 24, md: 24 },
        },
        {
          label: t('处理方案和结果负责人'),
          field: 'resultManager',
          fieldLabel: 'resultManagerName',
          component: 'TreeSelect',
          dynamicDisabled: info.value.ope == 2 || info.value.ope == '0' ? false : true,
          componentProps: {
            api: officeTreeData,
            params: { isLoadUser: true, userIdPrefix: '', isAll: true, isShowCode: true },
            canSelectParent: false,
            allowClear: true,
          },
        },
        {
          label: t('处理方案确认日期'),
          field: 'dealResultSunDate',
          component: 'DatePicker',
          dynamicDisabled: info.value.ope == 2 || info.value.ope == '0' ? false : true,
          componentProps: {
            format: 'YYYY-MM-DD HH:mm',
            showTime: { format: 'HH:mm' },
            allowClear: false,
          },
          labelWidth: 180,
        },
        {
          label: t('最终处理'),
          field: 'info5',
          component: 'FormGroup',
          colProps: { lg: 24, md: 24 },
        },
        {
          label: t('最终处理意见'),
          field: 'lastDealResult',
          component: 'InputTextArea',
          dynamicDisabled: info.value.ope == 3 || info.value.ope == '0' ? false : true,
          componentProps: {
            maxlength: 1000,
            rows: 4,
          },
          colProps: { lg: 24, md: 24 },
        },
        {
          label: t('最终结果'),
          field: 'lastSubResultType',
          component: 'Select',
          dynamicDisabled: info.value.ope == 3 || info.value.ope == '0' ? false : true,
          componentProps: {
            dictType: 'M8_task_check_result',
            allowClear: true,
          },
        },
        {
          label: t('最终检验人员'),
          field: 'lastSubCheckor',
          fieldLabel: 'lastSubCheckorName',
          component: 'TreeSelect',
          dynamicDisabled: info.value.ope == 3 || info.value.ope == '0' ? false : true,
          componentProps: {
            api: officeTreeData,
            params: { isLoadUser: true, userIdPrefix: '', isAll: true, isShowCode: true },
            canSelectParent: false,
            allowClear: true,
          },
        },
        {
          label: t('最终检验合格日期'),
          field: 'lastSubCheckDate',
          component: 'DatePicker',
          dynamicDisabled: info.value.ope == 3 || info.value.ope == '0' ? false : true,
          componentProps: {
            format: 'YYYY-MM-DD HH:mm',
            showTime: { format: 'HH:mm' },
            allowClear: false,
          },
        },
        {
          label: t('返修工时'),
          field: 'rsTime',
          component: 'Input',
          dynamicDisabled: info.value.ope == 3 || info.value.ope == '0' ? false : true,
          componentProps: {
            maxlength: 8,
          },
          rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
        },
        {
          label: t('返修金额'),
          field: 'rsMoney',
          component: 'Input',
          dynamicDisabled: info.value.ope == 3 || info.value.ope == '0' ? false : true,
          componentProps: {
            maxlength: 16,
          },
          rules: [
            {
              pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/,
              message: t('请输入一个数值'),
            },
          ],
        },
        {
          label: t('是否扣分'),
          field: 'isDedPoint',
          component: 'Select',
          dynamicDisabled: info.value.ope == 3 || info.value.ope == '0' ? false : true,
          componentProps: {
            dictType: 'sys_yes_no',
            maxlength: 10,
          },
        },
        {
          label: t('备注信息'),
          field: 'remarks',
          component: 'InputTextArea',
          dynamicDisabled: info.value.ope == 3 || info.value.ope == '0' ? false : true,
          componentProps: {
            maxlength: 500,
            rows: 4,
          },
          colProps: { lg: 24, md: 24 },
        },
        {
          label: t('当前阶段'),
          field: 'dealStatus',
          component: 'Select',
          dynamicDisabled: true,
          componentProps: {
            dictType: 'm8_check_deal_status',
            defaultValue: '0',
            maxlength: 10,
          },
          colProps: { lg: 24, md: 24 },
        },
        {
          label: t('附件上传'),
          field: 'dataMap',
          component: 'Upload',
          componentProps: {
            loadTime: computed(() => record.value.__t),
            bizKey: computed(() => record.value.id),
            bizType: 'm8OperCheckDeal_file',
            uploadType: 'image',
            maxSize: 100,
          },
          colProps: { lg: 24, md: 24 },
        },
        {
          label: t(''),
          field: 'detailInfo',
          component: 'FormGroup',
          colProps: { lg: 24, md: 24 },
        },
        {
          label: t('创建人'),
          field: 'createBy',
          component: 'Input',
          dynamicDisabled: true,
          componentProps: {
            maxlength: 64,
          },
          show: false,
        },
        {
          label: t('创建时间'),
          field: 'createDate',
          component: 'Input',
          dynamicDisabled: true,
          componentProps: {
            maxlength: 64,
          },
          show: false,
        },
      ],
      baseColProps: { lg: 12, md: 24 },
    });
    await resetFields();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false, showOkBtn: record.value.dealStatus == 4 ? false : true });
    if (info.value.ope == '1') {
      document.getElementById('info3').scrollIntoView();
    }
    if (info.value.ope == '2') {
      document.getElementById('info4').scrollIntoView();
    }
    if (info.value.ope == '3') {
      document.getElementById('info5').scrollIntoView();
    }
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        djNo: record.value.djNo,
      };
      data.ope = info.value.ope;
      const res = await m8OperCheckDelSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
